package com.zenithmind.library.pojo.domain;

import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.zenithmind.common.base.BaseEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDate;

/**
 * 出版社实体
 * 
 * @author ZenithMind
 * @since 2024-06-14
 */
@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
@NoArgsConstructor
@Schema(name = "出版社实体", description = "出版社信息")
@TableName("lib_publisher")
public class Publisher extends BaseEntity {

    @NotBlank(message = "出版社名称不能为空")
    @Size(max = 200, message = "出版社名称长度不能超过200个字符")
    @Schema(description = "出版社名称")
    private String name;

    @Size(max = 200, message = "出版社英文名长度不能超过200个字符")
    @Schema(description = "出版社英文名")
    private String nameEn;

    @Size(max = 50, message = "出版社编码长度不能超过50个字符")
    @Schema(description = "出版社编码")
    private String code;

    @Schema(description = "成立日期")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate foundedDate;

    @Size(max = 100, message = "所在国家长度不能超过100个字符")
    @Schema(description = "所在国家")
    private String country;

    @Size(max = 100, message = "所在城市长度不能超过100个字符")
    @Schema(description = "所在城市")
    private String city;

    @Schema(description = "详细地址")
    @Size(max = 500, message = "详细地址长度不能超过500个字符")
    private String address;

    @Schema(description = "联系电话")
    @Size(max = 50, message = "联系电话长度不能超过50个字符")
    private String phone;

    @Email(message = "邮箱格式不正确")
    @Size(max = 100, message = "邮箱长度不能超过100个字符")
    @Schema(description = "联系邮箱")
    private String email;

    @Schema(description = "官方网站")
    private String website;

    @Schema(description = "出版社简介")
    @Size(max = 2000, message = "出版社简介长度不能超过2000个字符")
    private String description;

    @Schema(description = "出版社Logo")
    private String logo;

    @Schema(description = "状态 (0-禁用, 1-启用)")
    private Integer status;

    /**
     * 判断是否启用
     */
    public boolean isEnabled() {
        return status != null && status == 1;
    }

    /**
     * 获取状态名称
     */
    public String getStatusName() {
        if (status == null) {
            return "未知";
        }
        return switch (status) {
            case 0 -> "禁用";
            case 1 -> "启用";
            default -> "未知";
        };
    }

    /**
     * 获取完整名称（包含英文名）
     */
    public String getFullName() {
        if (nameEn != null && !nameEn.trim().isEmpty()) {
            return name + " (" + nameEn + ")";
        }
        return name;
    }

    /**
     * 获取完整地址
     */
    public String getFullAddress() {
        StringBuilder fullAddress = new StringBuilder();

        if (country != null && !country.trim().isEmpty()) {
            fullAddress.append(country);
        }

        if (city != null && !city.trim().isEmpty()) {
            if (fullAddress.length() > 0) {
                fullAddress.append(", ");
            }
            fullAddress.append(city);
        }

        if (address != null && !address.trim().isEmpty()) {
            if (fullAddress.length() > 0) {
                fullAddress.append(", ");
            }
            fullAddress.append(address);
        }

        return fullAddress.toString();
    }

    /**
     * 判断是否有联系方式
     */
    public boolean hasContactInfo() {
        return (phone != null && !phone.trim().isEmpty()) ||
               (email != null && !email.trim().isEmpty()) ||
               (website != null && !website.trim().isEmpty());
    }
}
